package com.example.work.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.work.entity.UserEntity;
import com.example.work.service.UserService;
import com.example.work.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Description:    注册
 * Author:         JinMu
 * CreateDate:     2018/2/26 10:58
 * Version:        1.0
 */
@RestController
public class RegController {

    @Autowired
    private UserService userService;

    @PostMapping("/regcon")
    public String reg(@RequestBody JSONObject params){
        String name = params.getString("name");
        String password = params.getString("password");
        String password_md = MD5Util.getmd32(password);
        UserEntity userEntity = new UserEntity(name,password_md);
        if(name ==""|| password==""){
            return "false";
        }else {
            UserEntity u = userService.queryuser(name);
            if(u !=null){
                return "error";
            }else{
                userService.adduser(userEntity);
                return "success";
            }

        }
    }
    @PostMapping("/index")
    public String index(@RequestBody JSONObject params,HttpServletRequest request) {
        String name = params.getString("name");
        String password = params.getString("password");
        String pwd = MD5Util.getmd32(password);
        UserEntity u = userService.queryuser(name);
        if(u != null && u.getPassword() .equals(pwd) && u.getPassword() != ""){
            if(u.getKey() == null || u.getKey().equals("") || u.getMerchantID() == null || u.getMerchantID().equals("")){
                return "succ_err";
            }else {
                return "success";
            }
        }else {
            return "error";
        }

    }

}